import collections
import datetime
import time

from django.http import JsonResponse
from django.shortcuts import render
from django.db.models import Count
from web import models


def dashboard(request, project_id):
    """概览"""
    # 问题的数据处理
    status_dict = collections.OrderedDict()
    for key, text in models.Issue.status_choices:
        status_dict[key] = {'text': text, 'count': 0}
    issuse_data = models.Issue.objects.filter(project_id=project_id).values('status').annotate(ct=Count('id'))
    for item in issuse_data:
        status_dict[item['status']]['count'] = item['ct']

    # 项目成员
    user_list = models.ProjectUser.objects.filter(project_id=project_id).values('user_id', 'user__username')
    # 最近的10个问题
    top_ten = models.Issue.objects.filter(project_id=project_id, assign__isnull=False).order_by('-id')[0:10]
    context = {
        'status_dict': status_dict,
        'user_list': user_list,
        'top_ten_object': top_ten,
    }
    return render(request, 'dashboard.html', context)


def issues_chart(request, project_id):
    """概览页面生成higncharts所需的数据"""
    today = datetime.datetime.now().date()
    date_dict = collections.OrderedDict()
    for i in range(0, 30):
        date = today - datetime.timedelta(days=i)
        date_dict[date.strftime('%Y-%m-%d')] = [time.mktime(date.timetuple()) * 1000, 0]
    # 最近30天，每天创建的问题数量
    # 从数据库中提取最近30天的数据& 根据日期进行每天分组
    result=models.Issue.objects.filter(project_id=project_id, create_datetime__gte=today - datetime.timedelta(days=30)).extra(
        select={"ctime":"strftime('%%Y-%%m-%%d',web_issue.create_datetime)"}).values('ctime').annotate(ct=Count('id'))

    for item in result:
        date_dict[item['ctime']][1]=item['ct']
    return JsonResponse({'status': True, 'data': list(date_dict.values())})
